package com.bw.module_home.ui

import android.app.Activity
import android.app.TimePickerDialog
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.bw.lib_common.base.BaseActivity
import com.bw.module_home.R
import com.bw.module_home.databinding.ActivityAddDeviceBinding
import com.bw.module_home.device.model.DeviceEntityItem
import com.bw.module_home.device.viewmodel.DeviceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/device/add_device")
class AddDeviceActivity : BaseActivity<DeviceViewModel, ActivityAddDeviceBinding>() {
    private val vm: DeviceViewModel by viewModels()

    override val mLayoutResId: Int
        get() = R.layout.activity_add_device

    override fun initView() {
        super.initView()
        mBinding.deviceAddBack.setOnClickListener {
            finish()
        }
        val adapter = ArrayAdapter.createFromResource(
            this,
            R.array.spinner_device_items,
            android.R.layout.simple_spinner_item
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.deviceSpType.adapter = adapter
        mBinding.deviceSpType.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
                val selectedItem = parent.getItemAtPosition(position).toString()
                println("选中的项：$selectedItem")
                mBinding.deviceEtType.setText(selectedItem)
            }

            override fun onNothingSelected(parent: AdapterView<*>) {}
        }
        mBinding.btnSubmit.setOnClickListener {
            val deviceItem = DeviceEntityItem(
                id = 0,
                deviceName = mBinding.deviceEtName.text.toString(),
                deviceType = mBinding.deviceEtType.text.toString(),
                failureRate = mBinding.deviceEtLv.text.toString().toDouble(),
                installationLocation = mBinding.deviceEtAddress.text.toString(),
                status = mBinding.deviceEtType.text.toString()
            )
            vm.addDevice(deviceItem)
        }
        mBinding.deviceTimeView.setOnClickListener {
            TimePickerDialog(this, { _, hourOfDay, minute ->
                mBinding.deviceEtTime.text = "${hourOfDay}:${minute}"
            }, 0, 0, true).show()
        }
//        mBinding.deviceImage.setOnClickListener {
//            PictureSelector.create(this, 512).selectPicture()
//        }
    }

    override fun initListener() {
        super.initListener()
        vm.addDeviceSuccess.observe(this) {
            showToast("添加成功")
            finish()
        }
        vm.mErrorLiveData.observe(this) {
            showToast(it)
            Log.i("add_device", "loadData: $it")
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 512 && resultCode == Activity.RESULT_OK) {
//            val extras = data?.extras
//            val pictureBean = extras?.get(PictureSelector.PICTURE_RESULT) as? PictureBean
//            pictureBean?.path?.let { path ->
//                mBinding.deviceImage2.visibility = View.VISIBLE
//                Glide.with(this).load(path).into(mBinding.deviceImage2)
//            }
        }
    }
}